<?php
/**
 * （logic）日销量统计报表-品类 (erp-FBA品类库存)
 * svc:fab-stock
 */

namespace App\Logic\SalesReport;

use App\Models\Basics\SysPlat;
use App\Models\SalesReport\DailyCv;
use App\Models\SalesReport\DailySv;
use Illuminate\Support\Facades\DB;

class DailySalesVolumeClassifyUp
{
    /**
     * 日销量统计报表-更新FBA库存
     */
    public static function exe(){
        $platId = SysPlat::PLAT_AMAZON; //amazon
        DailyCv::query()
            ->where('plat_id',$platId)
            ->orderBy('id')
            ->chunk(800,function ($re) use($platId){
                DB::beginTransaction();
                try{
                    foreach($re as $_re){
                        if (intval($_re->plat_id) == $platId){
                            //日销量统计报表(品类)
                            $_re->fba_instant_num = self::getCvFBAInstant($_re->classify_id);
                            $_re->fba_transit_num = self::getCvFBATransit($_re->classify_id);
                            $_re->save();

                            foreach ($_re->cvs as $_cvs){
                                //品类日销量和库存-SKU
                                $_cvs->fba_instant_num = self::getCvsFBAInstant($_re->classify_id,$_cvs->psku_id);
                                $_cvs->fba_transit_num = self::getCvsFBATransit($_re->classify_id,$_cvs->psku_id);
                                $_cvs->save();
                            }
                            foreach ($_re->cva as $_cva){
                                //品类日销量和库存-站点
                                $_cva->fba_instant_num = self::getCvaFBAInstant($_re->classify_id,$_cva->area_id);
                                $_cva->fba_transit_num = self::getCvaFBATransit($_re->classify_id,$_cva->area_id);
                                $_cva->save();
                            }
                        }
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
            });
    }

    /**
     * FBA库存 (日销量统计报表(品类))
     * @param $classifyId
     * @return array
     */
    public static function getCvFBAInstant($classifyId){
        return DailySv::query()->where('classify_id',$classifyId)->sum('fba_instant_num');
    }

    /**
     * FBA在途 (日销量统计报表(品类))
     * @param $classifyId
     * @return mixed
     */
    public static function getCvFBATransit($classifyId){
        return DailySv::query()->where('classify_id',$classifyId)->sum('fba_transit_num');
    }

    /**
     * FBA库存 （品类日销量和库存-SKU）
     * @param $classifyId
     * @param $pSkuId
     * @return array
     */
    public static function getCvsFBAInstant($classifyId,$pSkuId){
        return DailySv::query()->where('classify_id',$classifyId)->where('psku_id',$pSkuId)->sum('fba_instant_num');
    }

    /**
     * FBA在途 （品类日销量和库存-SKU）
     * @param $classifyId
     * @param $pSkuId
     * @return mixed
     */
    public static function getCvsFBATransit($classifyId,$pSkuId){
        return DailySv::query()->where('classify_id',$classifyId)->where('psku_id',$pSkuId)->sum('fba_transit_num');
    }

    /**
     * FBA库存 （品类日销量和库存-站点）
     * @param $classifyId
     * @param $areaId
     * @return array
     */
    public static function getCvaFBAInstant($classifyId,$areaId){
        return DailySv::query()->where('classify_id',$classifyId)->where('area_id',$areaId)->sum('fba_instant_num');
    }

    /**
     * FBA在途 （品类日销量和库存-站点）
     * @param $classifyId
     * @param $areaId
     * @return mixed
     */
    public static function getCvaFBATransit($classifyId,$areaId){
        return DailySv::query()->where('classify_id',$classifyId)->where('area_id',$areaId)->sum('fba_transit_num');
    }
}
